package cn.uce.cc.srv.client;

import cn.uce.base.vo.BridgeVo;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.Map;
import java.util.TreeMap;

public class JsonHttpClient {
    private static String asskey;
    private static String comcode;
    private static String seckey;
    private static String version;





    private static final Logger logger = LoggerFactory.getLogger(JsonHttpClient.class);
    /**
     *桥接接口验签
     */
    public static String postJsonForOpenPlatBridge(String json) throws ParseException, IOException {
        BridgeVo bridgeVo = JSON.parseObject(json,BridgeVo.class);
        Map<String, Object> params = new TreeMap<>();
        params.put("accessKey", getAsskey());
        params.put("secretKey", getSeckey());
        params.put("compCode", getComcode());
        params.put("messageBody", JSON.toJSONString(bridgeVo.getParameters()));
        params.put("version", getVersion());
        params.put("timestamp", String.valueOf(System.currentTimeMillis()));
        String securityStr = "";
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(JSON.toJSONString(params).getBytes("UTF-8"));
            securityStr = new String(Base64.encodeBase64(messageDigest.digest()), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        params.put("digest", securityStr);
        params.put("serviceName", bridgeVo.getServiceName());
        params.put("parameters", bridgeVo.getParameters());
        json = JSON.toJSONString(params);
        return json;
    }


    public static String getAsskey() {
        return asskey;
    }

    public static void setAsskey(String asskey) {
        JsonHttpClient.asskey = asskey;
    }

    public static String getComcode() {
        return comcode;
    }

    public static void setComcode(String comcode) {
        JsonHttpClient.comcode = comcode;
    }

    public static String getSeckey() {
        return seckey;
    }

    public static void setSeckey(String seckey) {
        JsonHttpClient.seckey = seckey;
    }

    public static String getVersion() {
        return version;
    }

    public static void setVersion(String version) {
        JsonHttpClient.version = version;
    }
}
